package webv1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"myCloud/contrib"
	"myCloud/logsvr"
	"net/http"
)

var (
	abReq      = contrib.AbortReq
	check      = contrib.ArgsCheck
	getUser    = contrib.GetUser
	getRequest = contrib.GetRequestObject
	logger     = logsvr.GetLogger("webv1")
)

// 下面的内容运行在单独的进程中，若有需要，可比较方便的移植到 gateway 中
func ApiServerV2() {
	var err error
	gin.SetMode(gin.ReleaseMode)

	router := gin.New()
	router.Use(gin.Recovery())
	// zap logger
	router.Use(contrib.GinZapMiddleware(logger.Desugar()))

	router.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusFound, "/web/v1/portal")
	})
	webGroup := router.Group("/web/v1")
	webPageApiInit(webGroup)

	group := router.Group("/api/v1")
	notAuthApiInit(group)
	serverInfoApiInit(group)

	authGroup := router.Group("/api/v1", contrib.UserAuthorization())
	userApiInit(authGroup)
	albumV2ApiInit(authGroup)

	addr := fmt.Sprintf("0.0.0.0:%d", 18083)
	logger.Info(fmt.Sprintf("running at %s", addr))
	err = router.Run(addr)
	if err != nil {
		logger.Warn("run web server failed.", zap.Error(err))
	}
}
